import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;


public class imageSender extends Thread {
	private Camera cam;
	private networkHandler net;
	private Map map;
	
	public imageSender(networkHandler net, Map map) {
		this.cam = new Camera(400, 480);
		this.net = net;
		this.map = map;
	}

	/**
	 * @param args
	 */
	public void run() {
		/* Kristian added code here
		 * Good resolution : 640x480 and 480x400
		 * Working resolutions : [176x144] [320x240] [352x288] [480x400] [640x480] [1024x768]
		 */
		while(true) {
			try{
				ByteArrayOutputStream byteArrayO = null;
				byte [] byteArray = null;
	
				//net.getDout().writeInt(0);
				net.getDout().writeByte(0);
				net.getDout().flush();
				byteArrayO = new ByteArrayOutputStream();
				ImageIO.write(map.Yeild_Map(),"JPG",byteArrayO);
				byteArray = byteArrayO.toByteArray();
				net.getDout().writeInt(byteArray.length);
				net.getDout().write(byteArray);
				net.getDout().flush();
				
				net.getDout().writeByte(1);
				net.getDout().flush();
				byteArrayO = new ByteArrayOutputStream();
				ImageIO.write(cam.getimage(),"JPG",byteArrayO);
				byteArray = byteArrayO.toByteArray();
				net.getDout().writeInt(byteArray.length);
				net.getDout().write(byteArray);
				net.getDout().flush();
				
				Thread.sleep(250);
			}
			catch(IOException e){
				;
			}
			catch(InterruptedException e){
				;
			}
		}
		
		
	}

}
